package cn.dankal.flyaudio.personal;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;

import java.util.List;

import cn.dankal.basiclib.base.activity.BaseActivity;
import cn.dankal.basiclib.pojo.AddressEntity;
import cn.dankal.basiclib.util.ToastUtils;
import cn.dankal.basiclib.widget.ChoiceAddressDialog;
import cn.dankal.flyaudio.R;
import cn.dankal.flyaudio.contract.AddressManagerContract;
import cn.dankal.flyaudio.presenter.AddressManagerPresenter;
import cn.dankal.flyaudio.store.ConfirmOrderActivity;

public class AddAddressActivity extends BaseActivity implements AddressManagerContract.View, View.OnClickListener {

    private EditText personalNameView;

    private EditText personalMobileView;

    private EditText personalAddressDetailView;

    private Switch ivSwitch;

    private TextView personalAddressView;

    private TextView saveView;

    private String currentProvince;
    private String currentCity;
    private String currentArea;


    private AddressEntity entity;

    private AddressManagerPresenter mAddressManagerPresenter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_edit_address;
    }

    @Override
    protected void initComponents() {
        addSingleTitleBar("新建地址");
        mAddressManagerPresenter = new AddressManagerPresenter();
        entity = (AddressEntity) getIntent().getSerializableExtra("address");
        mAddressManagerPresenter.attachView(this);
        personalNameView = findViewById(R.id.et_address_personal_name);
        personalMobileView = findViewById(R.id.et_address_personal_mobile);
        personalAddressDetailView = findViewById(R.id.et_address_personal_address_detail);
        personalAddressView = findViewById(R.id.et_address_personal_address);
        saveView = findViewById(R.id.tv_save);
        saveView.setOnClickListener(this);
        personalAddressView.setOnClickListener(this);
        ivSwitch = findViewById(R.id.default_switch);

        initAddress();
    }

    private void initAddress() {
        if (entity != null) {
            saveView.setText("修改");
            ivSwitch.setChecked(entity.getDefaultAddress());
            currentProvince = entity.getProvince();
            currentCity = entity.getCity();
            currentArea = entity.getRegion();
            personalAddressView.setText(entity.getProvince() + entity.getCity() + entity.getRegion());
            personalAddressDetailView.setText(entity.getAddressDetail());
            personalNameView.setText(entity.getContacter());
            personalMobileView.setText(entity.getPhone());
            personalNameView.setSelection(personalNameView.getText().toString().length());
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mAddressManagerPresenter.detachView();
    }

    @Override
    public void handleAddressList(List<AddressEntity> addressEntities) {

    }

    @Override
    public void updateSuccess() {
        finishActivity();
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.et_address_personal_address:
                ChoiceAddressDialog dialog = new ChoiceAddressDialog(this, new ChoiceAddressDialog.OnConfirmAddressListener() {
                    @Override
                    public void onConfirm(String curProvice, String curCity, String curArea) {
                        currentProvince = curProvice;
                        currentCity = curCity;
                        currentArea = curArea;
                        personalAddressView.setText(curProvice + curCity + curArea);
                    }
                });
//                if(entity!=null){
//                    dialog.setDefault(entity.getProvince(),entity.getCity(),entity.getRegion());
//                }

                dialog.show();
                dialog.setTitle("选择所在地区");
                dialog.setDefault(currentProvince, currentCity, currentArea);
                break;
            case R.id.tv_save:
                AddressEntity entity = createParams();
                if (entity != null) {
                    if (this.entity == null) {
                        mAddressManagerPresenter.addAddress(entity);
                    } else {
                        mAddressManagerPresenter.UpdateAddress(entity, false);
                    }
                }
                break;

        }
    }

    private AddressEntity createParams() {
        String personalName = personalNameView.getText().toString().trim();
        String personalMobile = personalMobileView.getText().toString().trim();
        String personalDetailAddress = personalAddressDetailView.getText().toString().trim();
        if (TextUtils.isEmpty(personalName)) {
            ToastUtils.showShort("请输入联系人姓名");
            return null;
        }
        if (TextUtils.isEmpty(personalMobile)) {
            ToastUtils.showShort("请输入联系人手机号");
            return null;
        }
        if (personalMobile.length() != 11) {
            ToastUtils.showShort("请输入正确的手机号");
            return null;
        }
        if (TextUtils.isEmpty(personalDetailAddress)) {
            ToastUtils.showShort("请输入联系人详细地址");
            return null;
        }
        if (TextUtils.isEmpty(currentProvince) || TextUtils.isEmpty(currentCity) || TextUtils.isEmpty(currentArea)) {
            ToastUtils.showShort("请选择省市区");
            return null;
        }
        AddressEntity entity = new AddressEntity();
        if (this.entity != null) {
            entity.setUuid(this.entity.getUuid());
        }
        entity.setContacter(personalName);
        entity.setPhone(personalMobile);
        entity.setAddressDetail(personalDetailAddress);
        entity.setProvince(currentProvince);
        entity.setCity(currentCity);
        entity.setRegion(currentArea);
        entity.setDefaultAddress(ivSwitch.isChecked());
        return entity;
    }
}
